package com.yyz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyz.domain.Category;
import com.yyz.domain.Dish;
import com.yyz.domain.DishFlavor;
import com.yyz.domain.SetmealDish;
import com.yyz.dto.DishDto;
import com.yyz.mapper.CategoryMapper;
import com.yyz.mapper.DishMapper;
import com.yyz.service.DishFlavorService;
import com.yyz.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yyz
 * @create 2023-07-20 22:13
 */
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 判定该菜品分类是否关联了菜品
     *
     * @param ids
     * @return
     */
    @Override
    public Integer count(Long ids) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId, ids);

        Integer DishCount = dishMapper.selectCount(lqw);
        return DishCount;
    }

    /**
     * 添加菜品和口味
     *
     * @param dishDto
     */
    @Override
    public void insert(DishDto dishDto) {
        dishMapper.insert(dishDto);

        Long id = dishDto.getId();

        List<DishFlavor> list = dishDto.getFlavors();
        for (DishFlavor dishFlavor : list) {
            dishFlavor.setDishId(id);
        }

        dishFlavorService.insert(list);
    }

    /**
     * 分页查询菜品
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public IPage<DishDto> pageList(Integer page, Integer pageSize, String name) {
        IPage<Dish> pageInfo = new Page<>(page, pageSize);
        IPage<DishDto> dishDtoPage = new Page<>();

        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, Dish::getName, name).orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        dishMapper.selectPage(pageInfo, lqw);

        BeanUtils.copyProperties(pageInfo, dishDtoPage, "records");

        List<Dish> records = pageInfo.getRecords();
        List<DishDto> dishDtos = new ArrayList<>();
        for (Dish record : records) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(record, dishDto);

            Long id = record.getCategoryId();
            Category category = categoryMapper.selectById(id);

            dishDto.setCategoryName(category.getName());
            dishDtos.add(dishDto);
        }

        dishDtoPage.setRecords(dishDtos);

        return dishDtoPage;
    }

    /**
     * 根据id页面回显
     *
     * @param ids
     * @return
     */
    @Override
    public Dish selectById(Long ids) {
        Dish dish = dishMapper.selectById(ids);

        Long id = dish.getId();

        List<DishFlavor> dishFlavors = dishFlavorService.selectById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        dishDto.setFlavors(dishFlavors);

        return dishDto;
    }

    /**
     * 修改菜品
     *
     * @param dishDto
     */
    @Override
    @Transactional

    public void update(DishDto dishDto) {
        dishMapper.updateById(dishDto);

        List<DishFlavor> list = dishDto.getFlavors();
        Long id = dishDto.getId();
        for (DishFlavor dishFlavor : list) {
            dishFlavor.setDishId(id);
        }

        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, id);
        dishFlavorService.delete(lqw);

        dishFlavorService.update(list);
    }

    /**
     * 删除菜品
     */
    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            dishMapper.deleteById(id);
        }
    }

    /**
     * 停售 启售
     *
     * @param status
     * @param ids
     */
    @Override
    public void status(Integer status, Long[] ids) {
        for (Long id : ids) {
            Dish dish = dishMapper.selectById(id);

            dish.setStatus(status);

            dishMapper.updateById(dish);
        }
    }

    /**
     * 查询菜品分类下的菜品
     *
     * @param categoryId
     * @return
     */
    @Override
    public List<DishDto> list(Long categoryId) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId, categoryId).eq(Dish::getStatus, 1)
                .orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> dishes = dishMapper.selectList(lqw);
        List<DishDto> dishDtos = new ArrayList<>();
        for (Dish dish : dishes) {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);

            Long id = dish.getId();

            LambdaQueryWrapper<DishFlavor> dlqw = new LambdaQueryWrapper<>();
            dlqw.eq(DishFlavor::getDishId, id);

            List<DishFlavor> dishFlavors = dishFlavorService.selectList(dlqw);
            dishDto.setFlavors(dishFlavors);

            dishDtos.add(dishDto);
        }

        return dishDtos;
    }
}
